package com.sctu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.PalyFoodOneDetailsActivity;
import com.sctu.EMSApp.R;
import com.sctu.adapter.PlayFoodMoreListviewAdapter;
import com.sctu.bean.PlayFoodList;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

/**
 * Created by zhu on 2015/11/7.
 */
public class PlaySpecialFragment extends Fragment {
    private View view;
    private ListView listView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_play_special, container, false);
        initview();

        return view;

    }

    private void initview() {
listView = (ListView) view.findViewById(R.id.special_list);
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_SPRCIAL_LIST,listener,errorlistener);

    }


    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("Json", json);
            // 开始解析JSON
            // 反序列化，直接将JSON反序列化为Test这个Class对象
            final PlayFoodList results = JSON.parseObject(json,
                    PlayFoodList.class);
            listView.setAdapter(new PlayFoodMoreListviewAdapter(getActivity(), results));

            //监听listitem
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(),PalyFoodOneDetailsActivity.class);
                    intent.putExtra("id",results.getResults().get(i).getId());
                    startActivity(intent);
                }
            });
            //返回上一级

        }
    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };


}
